import { canvas, ctx, Game, enemyImage, szImage, ssImage } from './game-state.js';

// 添加图像加载函数
function loadImage(src) {
    return new Promise((resolve, reject) => {
        const img = new Image();
        img.onload = () => resolve(img);
        img.onerror = () => reject(new Error(`Failed to load image at ${src}`));
        img.src = src;
    });
}

// 在 Game 初始化时加载图像

export class Enemy {
    // 初始化敌人坐标（基于画布宽高的随机位置）
    constructor() {
        this.x = Math.random() * canvas.width;
        this.y = Math.random() * canvas.height;
        this.width = 120;
        this.height = 120;
        this.direction = Math.floor(Math.random() * 4);
        this.speed = 2;
        this.fireCooldown = 2000; // 射击冷却时间（毫秒）
        this.lastShot = 0;
        this.isHit = false; // 添加状态来跟踪是否被击中
        this.hitTime = 0; // 记录被击中的时间
        this.image = enemyImage; // 初始化 image 属性为 enemyImage
        this.health = 100; // 添加血量属性
    }

    move() {
        // 随机改变方向
        if (Math.random() < 0.02) {
            this.direction = Math.floor(Math.random() * 4);
        }

        // 根据方向移动
        switch (this.direction) {
            case 0: this.y -= this.speed; break; // 上
            case 1: this.x += this.speed; break; // 右
            case 2: this.y += this.speed; break; // 下
            case 3: this.x -= this.speed; break; // 左
        }

        // 只需要保持基本的移动逻辑
        this.x += this.speed * Math.cos(this.direction);
        this.y += this.speed * Math.sin(this.direction);
    }

    shoot() {
        const now = Date.now();
        if (now - this.lastShot > this.fireCooldown) {
            this.lastShot = now;
            return {
                x: this.x + this.width / 2,
                y: this.y + this.height / 2,
                direction: this.direction,
                speed: 7
            };
        }
        return null;
    }

    draw(ctx, screenX, screenY) {  // 添加参数
        ctx.save();
        // 使用传入的屏幕坐标
        ctx.translate(screenX, screenY);
        ctx.rotate(this.direction * Math.PI / 2);
        // 直接使用 this.image
        let imageToDraw = this.image;
        
        if (imageToDraw && imageToDraw.complete) {
            ctx.drawImage(imageToDraw, -this.width / 2, -this.height / 2, this.width, this.height);
        } else {
            // 如果图像尚未加载完成，可以绘制一个占位矩形或其他内容
            ctx.fillStyle = 'blue';
            ctx.fillRect(-this.width / 2, -this.height / 2, this.width, this.height);
        }
        ctx.restore();

        // 绘制血量条
        this.drawHealthBar(ctx, screenX, screenY);
    }

    // 新增方法：绘制血量条
    drawHealthBar(ctx, screenX, screenY) {
        ctx.save();
        ctx.translate(screenX, screenY);
        ctx.fillStyle = 'red';
        ctx.fillRect(-this.width / 2, -this.height / 2 - 10, this.width * (this.health / 100), 5);
        ctx.restore();
    }
}

export function drawEnemies(ctx, cameraX, cameraY) {
    Game.enemies.forEach(enemy => {
        enemy.move();
        const screenX = enemy.x - cameraX;
        const screenY = enemy.y - cameraY;
        enemy.draw(ctx, screenX, screenY);
    });
}